package com.itheima.health.service.impl;

import com.alibaba.dubbo.common.utils.StringUtils;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.dao.SetmealDao;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.exception.MyException;
import com.itheima.health.pojo.Setmeal;
import com.itheima.health.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @program: health_parent
 * @description:
 * @author: HuChunping
 * @create: 2021-01-10 17:56
 **/
@Service(interfaceClass = SetmealService.class)
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealDao setmealDao;

    /**
    * @Description: 添加检查套餐
    * @Param: [setmeal, checkgroupIds]
    * @return: void
    * @Author: HuChunping
    * @Date: 2021/1/10-17:56
    */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(Setmeal setmeal, Integer[] checkgroupIds) {
        setmealDao.add(setmeal);
        // 添加setmeal后获取返回的值，供添加中间表使用
        Integer setmealId = setmeal.getId();

        // 如果选中了检查组项，遍历添加到中间表
        if (checkgroupIds != null) {
            for (Integer checkgroupId : checkgroupIds) {
                setmealDao.addSetmealCheckGroup(setmealId ,checkgroupId);
            }
        }
    }

    /**
    * @Description: 查询检查套餐
    * @Param: [queryPageBean]
    * @return: com.itheima.health.entity.PageResult<com.itheima.health.pojo.Setmeal>
    * @Author: HuChunping
    * @Date: 2021/1/10-19:57
    */
    @Override
    public PageResult<Setmeal> findPage(QueryPageBean queryPageBean) {
        // 分页结果集，指定起始页，每页大小
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());

        // 如果查询语句不为空，进行模糊查询
        if (StringUtils.isNotEmpty(queryPageBean.getQueryString())) {
            queryPageBean.setQueryString("%" + queryPageBean.getQueryString() + "%");
        }

        // 查询分页结果，只需要查询语句即可，会被pageResult拦截,自动查询前面传的分页数据
        Page<Setmeal> pages = setmealDao.findPage(queryPageBean.getQueryString());
        return new  PageResult<Setmeal>(pages.getTotal(), pages.getResult());
    }

    /**
    * @Description: 查询套餐
    * @Param: [id]
    * @return: com.itheima.health.pojo.Setmeal
    * @Author: HuChunping
    * @Date: 2021/1/11-11:44
    */
    @Override
    public Setmeal findById(int id) {
        Setmeal setmeal = setmealDao.findById(id);
        return setmeal;
    }

    /**
    * @Description: 通过套餐id查询勾选中的检查组
    * @Param: [id]
    * @return: java.lang.Integer[]
    * @Author: HuChunping
    * @Date: 2021/1/11-14:52
    */
    @Override
    public List<Integer> findCheckGroupIdsBySetmealId(int id) {
        List<Integer> checkGroupIds = setmealDao.findCheckGroupIdsBySetmealId(id);
        return checkGroupIds;
    }

    /**
    * @Description: 编辑更新套餐
    * @Param: [setmeal, checkGroupIds]
    * @return: void
    * @Author: HuChunping
    * @Date: 2021/1/11-15:39
    */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(Setmeal setmeal, Integer[] checkGroupIds) {
        // 更新检查套餐
        setmealDao.update(setmeal);
        // 删除检查套餐，检查组中间表
        setmealDao.deleteBySetmealId(setmeal.getId());

        // 如果检查组不为空，添加勾选的检查组
        if (checkGroupIds != null) {
            for (Integer checkGroupId : checkGroupIds) {
                setmealDao.addSetmealCheckGroup(setmeal.getId(),checkGroupId);
            }
        }
    }

    /**
    * @Description: 通过套餐删除id
    * @Param: [id]
    * @return: void
    * @Author: HuChunping
    * @Date: 2021/1/11-16:40
    */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteById(int id) {
        // 判断套是否被套餐使用
        int count = setmealDao.findCountBySetmealId(id);
        // 通过套餐订单中间表查询套是否被订单使用
        if (count > 0){
            throw new MyException("该套餐被订单使用，不能删除");
        }

        // 没关系，先删除套餐与检查组关系（添加先添加单独的表再添加中间表。删除先删除中间表再删除单独的表）
        setmealDao.deleteSetmealCheckGroup(id);
        // 删除套餐
        setmealDao.deleteById(id);
    }

    /**
    * @Description:  查询所有数据库套餐的垃圾图片，便于七牛的垃圾图片移除
    * @Param: [] 
    * @return: java.util.List<java.lang.String> 
    * @Author: HuChunping 
    * @Date: 2021/1/12-16:05
    */
    @Override
    public List<String> findImgs() {
        List<String> imgs = setmealDao.findImgs();
        return imgs;
    }

    /**
    * @Description: 查询所有套餐，用于用户端套餐展示
    * @Param: []
    * @return: java.util.List<com.itheima.health.pojo.Setmeal>
    * @Author: HuChunping
    * @Date: 2021/1/15-17:39
    */
    @Override
    public List<Setmeal> findAll() {
        List<Setmeal> setmeals = setmealDao.findAll();
        return setmeals;
    }

    /**
    * @Description: 套餐详情页查询套餐详情
    * @Param: [id]
    * @return: java.util.List<com.itheima.health.pojo.Setmeal>
    * @Author: HuChunping
    * @Date: 2021/1/15-20:04
    */
    @Override
    public Setmeal findDetailById(int id) {
        Setmeal setmeal = setmealDao.findDetailById(id);
        return setmeal;
    }
}
